package ctm.javacode;

import java.io.* ;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/proSuggest.do")

public class proSuggest extends HttpServlet {
	dbBean db;
	
	String id = "", sex = "", age = "";
	String height = "", weightType = "", weight = "";	

	String type1 = null;
	String type2 = null;
	String type3 = null;
	String type4 = null;
	String type5 = null;
	String type6 = null;
	String type7 = null;
	String type8 = null;
	String type9 = null;
	String type10 = null;
	String type11 = null;
	String type12 = null;
	String type13 = null;
	String type14 = null;
	String type15 = null;
	String type16 = null;
	String type17 = null;
	
	String milk1 = null;
	String milk2 = null;
	String milk3 = null;
	String milk4 = null;
	String milk5 = null;
	String milk6 = null;
	String low_milk1 = null;
	String low_milk2 = null;
	String low_milk3 = null;
	String low_milk4 = null;
	String low_milk5 = null;
	String low_milk6 = null;
	String high_milk1 = null;
	String high_milk2 = null;
	String high_milk3 = null;
	String high_milk4 = null;
	String high_milk5 = null;
	String high_milk6 = null;
	String veg1 = null;
	String veg2 = null;
	String veg3 = null;
	String veg4 = null;
	String veg5 = null;
	String veg6 = null;
	String fruit1 = null;
	String fruit2 = null;
	String fruit3 = null;
	String fruit4 = null;
	String fruit5 = null;
	String fruit6 = null;
	String grain1 = null;
	String grain2 = null;
	String grain3 = null;
	String grain4 = null;
	String grain5 = null;
	String grain6 = null;
	String low_meat1 = null;
	String low_meat2 = null;
	String low_meat3 = null;
	String low_meat4 = null;
	String low_meat5 = null;
	String low_meat6 = null;
	String meat1 = null;
	String meat2 = null;
	String meat3 = null;
	String meat4 = null;
	String meat5 = null;
	String meat6 = null;
	String high_meat1 = null;
	String high_meat2 = null;
	String high_meat3 = null;
	String high_meat4 = null;
	String high_meat5 = null;
	String high_meat6 = null;
	String fat1  = null;
	String fat2  = null;
	String fat3  = null;
	String fat4  = null;
	String fat5  = null;
	String fat6 = null;

	String work = null, activity = null, pressure = null ;
	String g_kgBW = null, proportion_fat = null;
	String quantity_Milk = null; 
	String quantity_Veg = null;
	String quantity_Fruit = null;
	
	String nourish_list = null;
	String breakfast = null, lunch = null, dinner = null;
	String save = null, time = null;
	
	String assign1 = null, assign2 = null, assign3 = null;
	String assign4 = null, assign5 = null, assign6 = null;
	String assign7 = null, assign8 = null, assign9 = null;
	String assign10 = null, assign11 = null, assign12 = null;
	String assign13 = null, assign14 = null, assign15 = null;
	String assign16 = null, assign17 = null, assign18 = null;
			
	PrintWriter out;
     
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		
		response.setCharacterEncoding("UTF-8");
		db = new dbBean();
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();
		
		out = response.getWriter();

		id = request.getParameter("id");
		sex = request.getParameter("sex");		
		age = request.getParameter("age");
		height = request.getParameter("height");
		weightType = request.getParameter("weightType") ;
		weight = request.getParameter("weight");
		
		type1 = request.getParameter("type1");
		type2 = request.getParameter("type2");
		type3 = request.getParameter("type3");
		type4 = request.getParameter("type4");
		type5 = request.getParameter("type5");
		type6 = request.getParameter("type6");
		type7 = request.getParameter("type7");
		type8 = request.getParameter("type8");
		type9 = request.getParameter("type9");
		type10 = request.getParameter("type10");
		type11 = request.getParameter("type11");
		type12 = request.getParameter("type12");
		type13 = request.getParameter("type13");
		type14 = request.getParameter("type14");
		type15 = request.getParameter("type15");
		type16 = request.getParameter("type16");
		type17 = request.getParameter("type17");

		milk1 = request.getParameter("milk1");
		milk2 = request.getParameter("milk2");
		milk3 = request.getParameter("milk3");
		milk4 = request.getParameter("milk4");
		milk5 = request.getParameter("milk5");
		milk6 = request.getParameter("milk6");
		low_milk1 = request.getParameter("low_milk1");
		low_milk2 = request.getParameter("low_milk2");
		low_milk3 = request.getParameter("low_milk3");
		low_milk4 = request.getParameter("low_milk4");
		low_milk5 = request.getParameter("low_milk5");
		low_milk6 = request.getParameter("low_milk6");
		high_milk1 = request.getParameter("high_milk1");
		high_milk2 = request.getParameter("high_milk2");
		high_milk3 = request.getParameter("high_milk3");
		high_milk4 = request.getParameter("high_milk4");
		high_milk5 = request.getParameter("high_milk5");
		high_milk6 = request.getParameter("high_milk6");
		veg1 = request.getParameter("veg1");
		veg2 = request.getParameter("veg2");
		veg3 = request.getParameter("veg3");
		veg4 = request.getParameter("veg4");
		veg5 = request.getParameter("veg5");
		veg6 = request.getParameter("veg6");
		fruit1 = request.getParameter("fruit1");
		fruit2 = request.getParameter("fruit2");
		fruit3 = request.getParameter("fruit3");
		fruit4 = request.getParameter("fruit4");
		fruit5 = request.getParameter("fruit5");
		fruit6 = request.getParameter("fruit6");
		grain1 = request.getParameter("grain1");
		grain2 = request.getParameter("grain2");
		grain3 = request.getParameter("grain3");
		grain4 = request.getParameter("grain4");
		grain5 = request.getParameter("grain5");
		grain6 = request.getParameter("grain6");
		low_meat1 = request.getParameter("low_meat1");
		low_meat2 = request.getParameter("low_meat2");
		low_meat3 = request.getParameter("low_meat3");
		low_meat4 = request.getParameter("low_meat4");
		low_meat5 = request.getParameter("low_meat5");
		low_meat6 = request.getParameter("low_meat6");
		meat1 = request.getParameter("meat1");
		meat2 = request.getParameter("meat2");
		meat3 = request.getParameter("meat3");
		meat4 = request.getParameter("meat4");
		meat5 = request.getParameter("meat5");
		meat6 = request.getParameter("meat6");
		high_meat1 = request.getParameter("high_meat1");
		high_meat2 = request.getParameter("high_meat2");
		high_meat3 = request.getParameter("high_meat3");
		high_meat4 = request.getParameter("high_meat4");
		high_meat5 = request.getParameter("high_meat5");
		high_meat6 = request.getParameter("high_meat6");
		fat1 = request.getParameter("fat1");
		fat2 = request.getParameter("fat2");
		fat3 = request.getParameter("fat3");
		fat4 = request.getParameter("fat4");
		fat5 = request.getParameter("fat5");
		fat6 = request.getParameter("fat6");
	
		work = request.getParameter("work") ;
		activity = request.getParameter("activity") ;
		pressure = request.getParameter("pressure") ;
		g_kgBW = request.getParameter("g_kgBW") ;
		proportion_fat = request.getParameter("proportion_fat") ;
		quantity_Milk = request.getParameter("quantity_Milk") ;
		quantity_Veg = request.getParameter("quantity_Veg") ;
		quantity_Fruit = request.getParameter("quantity_Fruit") ;
		
		nourish_list = request.getParameter("nourish_list");
		breakfast = request.getParameter("proportion_breakfast");
		lunch = request.getParameter("proportion_lunch");
		dinner = request.getParameter("proportion_dinner");
		
		assign1 = request.getParameter("breakfast_dessert_Milk");
		assign2 = request.getParameter("lunch_dessert_Milk");
		assign3 = request.getParameter("dinner_dessert_Milk");
		assign4 = request.getParameter("breakfast_dessert_Veg");
		assign5 = request.getParameter("lunch_dessert_Veg");
		assign6 = request.getParameter("dinner_dessert_Veg");
		assign7 = request.getParameter("breakfast_dessert_Fruit");
		assign8 = request.getParameter("lunch_dessert_Fruit");
		assign9 = request.getParameter("dinner_dessert_Fruit");
		assign10 = request.getParameter("breakfast_dessert_Grain");
		assign11 = request.getParameter("lunch_dessert_Grain");
		assign12 = request.getParameter("dinner_dessert_Grain");
		assign13 = request.getParameter("breakfast_dessert_Meat");
		assign14 = request.getParameter("lunch_dessert_Meat");
		assign15 = request.getParameter("dinner_dessert_Meat");
		assign16 = request.getParameter("breakfast_dessert_Fat");
		assign17 = request.getParameter("lunch_dessert_Fat");
		assign18 = request.getParameter("dinner_dessert_Fat");
		
		
		if (request.getParameter("save").equals("F"))
		{
			time = request.getParameter("time");
			if (request.getParameter("type").equals("T"))
				save = new String("T");
			else
				save = new String("F");
			UpdateData();
		}
		else 
		{
			if (request.getParameter("type").equals("T"))
				save = new String("T");
			else
				save = new String("F");
			
			CreateData();
		}
	}

	private void UpdateData() {
		Date date = Calendar.getInstance().getTime();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d kk:mm:ss");
	    
		String sqlQuery1 = 
			"DELETE FROM `history` WHERE id='" + id + "' && time='" + time + "'";
		String sqlQuery = 
			"INSERT INTO `history`(time, id, sex, age, height, weightType, weight, " + 
			"milk1, milk2, milk3, milk4, milk5, milk6, " +
			"low_milk1, low_milk2, low_milk3, low_milk4, low_milk5, low_milk6, " +
			"high_milk1, high_milk2, high_milk3, high_milk4, high_milk5, high_milk6, " +
			"veg1, veg2, veg3, veg4, veg5, veg6, " +
			"fruit1, fruit2, fruit3, fruit4, fruit5, fruit6, " +
			"grain1, grain2, grain3, grain4, grain5, grain6, " +
			"low_meat1, low_meat2, low_meat3, low_meat4, low_meat5, low_meat6, " +
			"meat1, meat2, meat3, meat4, meat5, meat6, " +
			"high_meat1, high_meat2, high_meat3, high_meat4, high_meat5, high_meat6, " +
			"fat1, fat2, fat3, fat4, fat5, fat6, " +
			"work, act, press, g_kgBW, fat_rate, " +
			"quantity_Milk, quantity_Veg, quantity_Fruit, " +
			"type1, type2, type3, type4, type5, type6, type7, type8, type9, type10, " +
			"type11, type12, type13, type14, type15, type16, type17, nourish_list, " +
			"breakfast, lunch, dinner, save, assign1, assign2, assign3, assign4, assign5, " +
			"assign6, assign7, assign8, assign9, assign10, assign11, assign12, assign13, " +
			"assign14, assign15, assign16, assign17, assign18" +
			") VALUES ('" +
			sdf.format(date) + "', '"  + id + "', '" + sex + "', '" + age + "', '" + height + "', '" + weightType + "', '" + weight + "', '" +
			milk1 + "', '" + milk2 + "', '" + milk3 + "', '" + milk4 + "', '" + milk5 + "', '" + milk6 + "', '" +
			low_milk1 + "', '" + low_milk2 + "', '" + low_milk3 + "', '" + low_milk4 + "', '" + low_milk5 + "', '" + low_milk6 + "', '" + 
			high_milk1 + "', '" + high_milk2 + "', '" + high_milk3 + "', '" + high_milk4 + "', '" + high_milk5 + "', '" + high_milk6 + "', '" +
			veg1 + "', '" + veg2 + "', '" + veg3 + "', '" + veg4 + "', '" + veg5 + "', '" + veg6 + "', '" + 
			fruit1 + "', '" + fruit2 + "', '" + fruit3 + "', '" + fruit4 + "', '" + fruit5 + "', '" + fruit6 + "', '" + 
			grain1 + "', '" + grain2 + "', '" + grain3 + "', '" + grain4 + "', '" + grain5 + "', '" + grain6 + "', '" + 
			low_meat1 + "', '" + low_meat2 + "', '" + low_meat3 + "', '" + low_meat4 + "', '" + low_meat5 + "', '" + low_meat6 + "', '" +
			meat1 + "', '" + meat2 + "', '" + meat3 + "', '" + meat4 + "', '" + meat5 + "', '" + meat6 + "', '" + 
			high_meat1 + "', '" + high_meat2 + "', '" + high_meat3 + "', '" + high_meat4 + "', '" + high_meat5 + "', '" + high_meat6 + "', '" + 
			fat1 + "', '" + fat2 + "', '" + fat3 + "', '" + fat4 + "', '" + fat5 + "', '" + fat6 + "', '" +
			work + "', '" + activity + "', '" + pressure +  "', '" + g_kgBW + "', '"  + proportion_fat + "', '"  +
			quantity_Milk + "', '" + quantity_Veg + "', '" + quantity_Fruit + "', '" +
			type1 + "', '" + type2 + "', '" + type3 + "', '" + type4 + "', '" + type5 + "', '" + type6 + "', '" +
			type7 + "', '" + type8 + "', '" + type9 + "', '" + type10 + "', '" + type11 + "', '" + type12 + "', '" +
			type13 + "', '" + type14 + "', '" + type15 + "', '" + type16 + "', '" + type17 + "', '" + nourish_list + "', '" +
			breakfast + "', '" + lunch + "', '" + dinner + "', '" + save + "', '" + assign1 + "', '" + assign2 + "', '" +
			assign3 + "', '" + assign4 + "', '" + assign5 + "', '" + assign6 + "', '" + assign7 + "', '" + assign8 + "', '" +
			assign9 + "', '" + assign10 + "', '" + assign11 + "', '" + assign12 + "', '" + assign13 + "', '" + assign14 + "', '" +
			assign15 + "', '" + assign16 + "', '" + assign17 + "', '" + assign18 +
			"')" ;
		
		try {
			db.ExcuteUpdate(sqlQuery1);		
			db.ExcuteUpdate(sqlQuery);		
			out.print("已存取");
			db.CloseConn();
		} catch(SQLException e) {
			//System.out.println(sqlQuery);
			out.print("存取失敗");
		} // end try-catch
	}

	private void CreateData() {
		// TODO Auto-generated method stub
		Date date = Calendar.getInstance().getTime();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d kk:mm:ss");

	    String sqlQuery = 
				"INSERT INTO `history`(time, id, sex, age, height, weightType, weight, " + 
				"milk1, milk2, milk3, milk4, milk5, milk6, " +
				"low_milk1, low_milk2, low_milk3, low_milk4, low_milk5, low_milk6, " +
				"high_milk1, high_milk2, high_milk3, high_milk4, high_milk5, high_milk6, " +
				"veg1, veg2, veg3, veg4, veg5, veg6, " +
				"fruit1, fruit2, fruit3, fruit4, fruit5, fruit6, " +
				"grain1, grain2, grain3, grain4, grain5, grain6, " +
				"low_meat1, low_meat2, low_meat3, low_meat4, low_meat5, low_meat6, " +
				"meat1, meat2, meat3, meat4, meat5, meat6, " +
				"high_meat1, high_meat2, high_meat3, high_meat4, high_meat5, high_meat6, " +
				"fat1, fat2, fat3, fat4, fat5, fat6, " +
				"work, act, press, g_kgBW, fat_rate, " +
				"quantity_Milk, quantity_Veg, quantity_Fruit, " +
				"type1, type2, type3, type4, type5, type6, type7, type8, type9, type10, " +
				"type11, type12, type13, type14, type15, type16, type17, nourish_list, " +
				"breakfast, lunch, dinner, save, assign1, assign2, assign3, assign4, assign5, " +
				"assign6, assign7, assign8, assign9, assign10, assign11, assign12, assign13, " +
				"assign14, assign15, assign16, assign17, assign18" +
				") VALUES ('" +
				sdf.format(date) + "', '"  + id + "', '" + sex + "', '" + age + "', '" + height + "', '" + weightType + "', '" + weight + "', '" +
				milk1 + "', '" + milk2 + "', '" + milk3 + "', '" + milk4 + "', '" + milk5 + "', '" + milk6 + "', '" +
				low_milk1 + "', '" + low_milk2 + "', '" + low_milk3 + "', '" + low_milk4 + "', '" + low_milk5 + "', '" + low_milk6 + "', '" + 
				high_milk1 + "', '" + high_milk2 + "', '" + high_milk3 + "', '" + high_milk4 + "', '" + high_milk5 + "', '" + high_milk6 + "', '" +
				veg1 + "', '" + veg2 + "', '" + veg3 + "', '" + veg4 + "', '" + veg5 + "', '" + veg6 + "', '" + 
				fruit1 + "', '" + fruit2 + "', '" + fruit3 + "', '" + fruit4 + "', '" + fruit5 + "', '" + fruit6 + "', '" + 
				grain1 + "', '" + grain2 + "', '" + grain3 + "', '" + grain4 + "', '" + grain5 + "', '" + grain6 + "', '" + 
				low_meat1 + "', '" + low_meat2 + "', '" + low_meat3 + "', '" + low_meat4 + "', '" + low_meat5 + "', '" + low_meat6 + "', '" +
				meat1 + "', '" + meat2 + "', '" + meat3 + "', '" + meat4 + "', '" + meat5 + "', '" + meat6 + "', '" + 
				high_meat1 + "', '" + high_meat2 + "', '" + high_meat3 + "', '" + high_meat4 + "', '" + high_meat5 + "', '" + high_meat6 + "', '" + 
				fat1 + "', '" + fat2 + "', '" + fat3 + "', '" + fat4 + "', '" + fat5 + "', '" + fat6 + "', '" +
				work + "', '" + activity + "', '" + pressure +  "', '" + g_kgBW + "', '"  + proportion_fat + "', '"  +
				quantity_Milk + "', '" + quantity_Veg + "', '" + quantity_Fruit + "', '" +
				type1 + "', '" + type2 + "', '" + type3 + "', '" + type4 + "', '" + type5 + "', '" + type6 + "', '" +
				type7 + "', '" + type8 + "', '" + type9 + "', '" + type10 + "', '" + type11 + "', '" + type12 + "', '" +
				type13 + "', '" + type14 + "', '" + type15 + "', '" + type16 + "', '" + type17 + "', '" + nourish_list + "', '" +
				breakfast + "', '" + lunch + "', '" + dinner + "', '" + save + "', '" + assign1 + "', '" + assign2 + "', '" +
				assign3 + "', '" + assign4 + "', '" + assign5 + "', '" + assign6 + "', '" + assign7 + "', '" + assign8 + "', '" +
				assign9 + "', '" + assign10 + "', '" + assign11 + "', '" + assign12 + "', '" + assign13 + "', '" + assign14 + "', '" +
				assign15 + "', '" + assign16 + "', '" + assign17 + "', '" + assign18 +
				"')" ;
	
		try {
			db.ExcuteUpdate(sqlQuery);			
			out.print("已存取");
			db.CloseConn();
		} catch(SQLException e) {
			System.out.println(sqlQuery);
			out.print("存取失敗");
		} // end try-catch	
	}
}